package com.huawei.csi.tool.log;

import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.core.AppenderBase;
import com.huawei.csi.tool.util.TimeUtil;
import javafx.application.Platform;
import javafx.scene.control.TextArea;

/**
 * @author lxw
 */
public class JavaFXAppender extends AppenderBase<ILoggingEvent> {
    private final TextArea textArea;

    public JavaFXAppender(TextArea textArea) {
        this.textArea = textArea;
    }

    @Override
    protected void append(ILoggingEvent event) {
        Platform.runLater(() -> {
            String message = "[" + event.getLevel().levelStr + "] " + "[" + TimeUtil.formatTimestamp(event.getTimeStamp()) + "]" + " - " + event.getFormattedMessage() + "\n";
            textArea.appendText(message);
        });
    }
}
